/*
 * Copyright (c) 2005 -2008 Reliable Source, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1,
 * BSD, and Eclipse Public License v1.0 which accompanies this
 * distribution and is available at:
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 * 		http://www.opensource.org/licenses/bsd-license.php
 * 		http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *     IBM Corporation	     -  http://ibm.com
 *     	- initial API and implementation
 *
 *     patientos.org         - http://patientos.org
 *     	- domain and database persistence layer
 *
 *     Reliable Source, Inc. - http://reliablesource.org
 *     	- Symbiosys Application Framework,
 *     	- Symbiosys Application, Symbiosys Product
 *
 */
package org.reliablesource.client.ui.symbiosys.platform;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

/**
 *
 * ApplicationActionBarAdvisor.java<br>
 * Created on: Jun 27, 2008<br>
 *
 * An action bar advisor is responsible for creating, adding, and disposing of
 * the actions added to a workbench window. Each window will be populated with
 * new actions.
 *
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 *
 * @category
 * @since 1.0
 */
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
    // Actions - important to allocate these only in makeActions, and then use
    // them
    // in the fill methods. This ensures that the actions aren't recreated
    // when fillActionBars is called with FILL_PROXY.
    private IWorkbenchAction exitAction;
    private IWorkbenchAction showViewMenu;
    private IWorkbenchAction preferences;
    private IWorkbenchAction find;
    private IWorkbenchAction cut;
    private IWorkbenchAction copy;
    private IWorkbenchAction paste;
    private IWorkbenchAction selectAll;
    private IWorkbenchAction about;
    private IWorkbenchAction helpContents;
    private IWorkbenchAction helpSearch;
    private IWorkbenchAction save;
    private IWorkbenchAction saveAll;
    private IWorkbenchAction saveAs;
    private IWorkbenchAction print;
    
    public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
        super(configurer);
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.application.ActionBarAdvisor#makeActions(org.eclipse.ui.IWorkbenchWindow)
     */
    protected void makeActions(final IWorkbenchWindow _window) {
	
        // Creates the actions and registers them.
        // Registering is needed to ensure that key bindings work.
        // The corresponding commands keybindings are defined in the plugin.xml
        // file.
        // Registering also provides automatic disposal of the actions when
        // the window is closed.
	
	ArrayList<IWorkbenchAction> _actions = new ArrayList<IWorkbenchAction>();
        exitAction = ActionFactory.QUIT.create(_window);
        _actions.add(exitAction);
        
        showViewMenu = ActionFactory.SHOW_VIEW_MENU.create(_window);
        _actions.add(showViewMenu);
        
        preferences = ActionFactory.PREFERENCES.create(_window);
        _actions.add(preferences);
        
        find = ActionFactory.FIND.create(_window);
        _actions.add(find);
        cut = ActionFactory.CUT.create(_window);
        _actions.add(cut);
        copy = ActionFactory.COPY.create(_window);
        _actions.add(copy);
        paste = ActionFactory.PASTE.create(_window);
        _actions.add(paste);
        selectAll = ActionFactory.SELECT_ALL.create(_window);
        _actions.add(selectAll);
        save = ActionFactory.SAVE.create(_window);
        _actions.add(save);
        saveAll = ActionFactory.SAVE_ALL.create(_window);
        _actions.add(saveAll);
        saveAs = ActionFactory.SAVE_AS.create(_window);
        _actions.add(saveAs);
        print = ActionFactory.PRINT.create(_window);
        _actions.add(print);
        about = ActionFactory.ABOUT.create(_window);
        _actions.add(about);
        helpContents = ActionFactory.HELP_CONTENTS.create(_window);
        _actions.add(helpContents);
        helpSearch = ActionFactory.HELP_SEARCH.create(_window);
        _actions.add(helpSearch);
        
        Iterator<IWorkbenchAction> _it = _actions.iterator();
        while(_it.hasNext())
            register(_it.next());
    }

    protected void fillMenuBar(IMenuManager _menuBar) {

	MenuManager _mgr = createWindowMenu("&File",
		IWorkbenchActionConstants.M_FILE);

	_mgr.add(save);
	_mgr.add(saveAll);
	_mgr.add(saveAs);
	_mgr.add(print);

	_mgr.add(exitAction);
	_menuBar.add(_mgr);

	// -------------------------------
	_mgr = createWindowMenu("&Edit", IWorkbenchActionConstants.M_EDIT);
	_mgr.add(find);
	_mgr.add(cut);
	_mgr.add(copy);
	_mgr.add(paste);
	_mgr.add(selectAll);
	_menuBar.add(_mgr);

	// -------------------------------

	_mgr = createWindowMenu("&Window", IWorkbenchActionConstants.M_WINDOW);
	_mgr.add(showViewMenu);
	_mgr.add(preferences);
	_menuBar.add(_mgr);
	// -------------------------------
	_mgr = createWindowMenu("&Help", IWorkbenchActionConstants.M_HELP);
	_mgr.add(helpContents);
	_mgr.add(helpSearch);
	_mgr.add(about);

	_menuBar.add(_mgr);
    }

    private MenuManager createWindowMenu(String _label, String _id){
	MenuManager _mgr = new MenuManager(_label, _id);
	return _mgr;
    }
}
